import {submit} from "../detect";

Page({
    data: {
        tagOptions: [
            '健康',
            '国际',
            '生活',
            '政治与政策',
            '科技资安',
            '科学研究',
            '环境能源',
            '科技',
            '医药健康',
            '灾难事故',
            '文体娱乐',
            '社会生活',
            '军事',
            '政治',
            '教育考试',
            '财经商业',
            '其他'
        ],
        title: '',
        use_llm: false,
        content: '',
        tag: '健康',
        url: "",
        tagIndex: 0,
        contentLength: 0,
        tooltipVisible: false
    },

    // 内容输入监听
    handleContentInput(e) {
        this.setData({
            content: e.detail.value,
            contentLength: e.detail.value.length
        })
    },

    onLoad() {
        const newsInfo = wx.getStorageSync('news_info')
        const newsUrl = wx.getStorageSync('news_url')
        if (newsInfo && newsUrl) {
            this.setData({
                content: newsInfo.content,
                title: newsInfo.title,
                tagIndex: newsInfo.tagIndex,
                contentLength: newsInfo.contentLength,
                url: newsUrl
            })
        }
    },
    onUnload() {
        // 清理页面资源
        wx.removeStorageSync('news_info')
        wx.removeStorageSync('news_url')
    },
    onTagChange(e) {
        const index = e.detail.value;
        this.setData({
            tagIndex: index,
            tag: this.data.tagOptions[index] // 将选中的标签赋值给 tag
        });
    },

    // 表单提交
    submitForm(e) {
        const {title, content} = e.detail.value
        const tagIndex = this.data.tagIndex
        console.log(tagIndex)
        if (!title || !content) {
            wx.showToast({title: '请填写完整信息', icon: 'none'})
            return
        }

        if (content.length < 10) {
            wx.showToast({title: '内容不少于10字', icon: 'none'})
            return
        }

        let news = {
            content,
            title,
            tag: tagIndex,
            url: this.data.url || ""
        }

        submit(news,"/pages/detect/common/index")
        // 表单提交
    }
})
